package cn.xiaoliu.pet.service.impl;

import cn.xiaoliu.basic.service.impl.BaseServiceImpl;
import cn.xiaoliu.basic.utils.CodeGenerateUtils;
import cn.xiaoliu.basic.utils.DistanceUtil;
import cn.xiaoliu.basic.utils.PageList;
import cn.xiaoliu.basic.utils.Point;
import cn.xiaoliu.order.domain.PetAcquisitionOrder;
import cn.xiaoliu.order.mapper.PetAcquisitionOrderMapper;
import cn.xiaoliu.org.domain.Employee;
import cn.xiaoliu.org.domain.Shop;
import cn.xiaoliu.org.mapper.EmployeeMapper;
import cn.xiaoliu.org.mapper.ShopMapper;
import cn.xiaoliu.pet.domain.Pet;
import cn.xiaoliu.pet.domain.PetDetail;
import cn.xiaoliu.pet.domain.SearchMasterMsg;
import cn.xiaoliu.pet.mapper.PetDetailMapper;
import cn.xiaoliu.pet.mapper.PetMapper;
import cn.xiaoliu.pet.mapper.SearchMasterMsgMapper;
import cn.xiaoliu.pet.query.SearchMasterMsgQuery;
import cn.xiaoliu.pet.service.IPetDetailService;
import cn.xiaoliu.pet.service.IPetService;
import cn.xiaoliu.pet.service.ISearchMasterMsgService;
import cn.xiaoliu.user.domain.Logininfo;
import cn.xiaoliu.user.domain.User;
import cn.xiaoliu.user.mapper.LogininfoMapper;
import cn.xiaoliu.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IPetService petService;
    @Autowired
    private IPetDetailService petDetailService;
    @Autowired
    private PetAcquisitionOrderMapper petAcquisitionOrderMapper;

    /**
     * 发布寻主信息
     * @param masterMsg
     * @param logininfo
     */
    @Override
    public void publish(SearchMasterMsg masterMsg, Logininfo logininfo) {
        //1.通过logininfo找到当前登录人（user）
        Long id = logininfo.getId();
        User user = userMapper.loadByLogininfoId(id);
        //2.系统自动找寻最近的店铺
        String address = masterMsg.getAddress();
        //调用工具类
        Point point = DistanceUtil.getPoint(address);
        //查询店铺
        List<Shop> shops = shopMapper.loadAll();
        //找到离寻主信息最近的店铺
        Shop nearestShop = DistanceUtil.getNearestShop(point, shops);
        //3.保存寻主消息
        masterMsg.setShop_id(nearestShop.getId());
        masterMsg.setUser_id(user.getId());
        searchMasterMsgMapper.save(masterMsg);

    }
    //查询宠物待处理信息
    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery query, Logininfo logininfo) {
        //获取当前登录人
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        query.setShopId(employee.getShop_id());
        query.setState(0);
        return super.queryPage(query);
    }
    //查询宠物已经处理信息
    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery query, Logininfo logininfo) {
        //获取当前登录人
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        query.setShopId(employee.getShop_id());
        query.setState(1);
        return super.queryPage(query);
    }
    //查询用户自己发布的信息
    @Override
    public PageList<SearchMasterMsg> queryUserText(SearchMasterMsgQuery query, Logininfo logininfo) {
        //查询当前登录人
        User user = userMapper.loadByLogininfoId(logininfo.getId());
        query.setUserId(user.getId());
        return super.queryPage(query);
    }
    //处理寻主信息
    @Override
    public void handle(Pet pet, Logininfo logininfo) {
        //处理过后将所有宠物的状态改为1
        searchMasterMsgMapper.updateState2ProcessedById(pet.getSearch_master_msg_id());
        //创建宠物
        petService.add(pet);

        //二.生成收购订单
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(pet.getSearch_master_msg_id());
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        PetAcquisitionOrder order = pet2order(pet,searchMasterMsg,employee.getId());
        petAcquisitionOrderMapper.save(order);


        //@TODO 支付


    }


    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg searchMasterMsg,Long empId) {
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
        order.setState(0);//待支付
        order.setPrice(pet.getCostprice());
        order.setAddress(searchMasterMsg.getAddress());
        String orderSn = CodeGenerateUtils.generateOrderSn(searchMasterMsg.getUser_id());
        order.setOrderSn(orderSn);
        order.setPet_id(pet.getId());
        order.setUser_id(searchMasterMsg.getUser_id());
        order.setPaytype(pet.getPayType());
        order.setShop_id(pet.getShop_id());
        order.setEmp_id(empId);
        return order;
    }
}
